# @Time : 2024-09-29 15:29
# @Author : Fioman 
# @Phone : 13149920693
"""
文件目录相关的操作模块,用来操作文件和目录
"""
import os

import app_config
from app.device_state import logger
from common.data_result import DataResult
from common.datetime_helper import get_datetime_for_logger_dir


def open_dir(dir_path):
    """
    打开目录,根据目录的路径名称
    :param dir_path: 
    :return: 
    """
    result = DataResult()
    try:
        ret = os.system("explorer.exe {}".format(dir_path))
        if ret != 1:
            result.state = False
            result.info = ret
    except Exception as e:
        result.state = False
        result.info = f"in open_dir() error: {str(e)}"
    finally:
        return result


def create_image_dir_by_order_id(order_id, joint_number):
    """
    根据工单Id创建生产作业使用的图像目录
    """
    dir_list = []
    root_dir = r"C:\raw"
    root_dir = os.path.join(root_dir, r"{}\{}\{}pin".format(
        get_datetime_for_logger_dir(), order_id, joint_number))
    if joint_number == "2":
        app_config.first_cut_board_dir_2pin = os.path.join(root_dir, r"first_cut")
        dir_list.append(app_config.first_cut_board_dir_2pin)
        app_config.size_check_ok_dir_2pin = os.path.join(root_dir, r"first_cut\size_check_ok")
        dir_list.append(app_config.size_check_ok_dir_2pin)
        app_config.raw_ok_dir_2pin = os.path.join(root_dir, r"raw_ok")
        dir_list.append(app_config.raw_ok_dir_2pin)
        app_config.raw_cut_dir_2pin = os.path.join(root_dir, r"raw_cut")
        dir_list.append(app_config.raw_cut_dir_2pin)
        app_config.totalboard_error_dir_2pin = os.path.join(root_dir, r"totalboard_error")
        dir_list.append(app_config.totalboard_error_dir_2pin)
        app_config.raw_fail_dir_2pin = os.path.join(root_dir, r"raw_fail")
        dir_list.append(app_config.raw_fail_dir_2pin)
    else:
        app_config.first_cut_board_dir_1pin = os.path.join(root_dir, r"first_cut")
        dir_list.append(app_config.first_cut_board_dir_1pin)
        app_config.size_check_ok_dir_1pin = os.path.join(root_dir, r"first_cut\size_check_ok")
        dir_list.append(app_config.size_check_ok_dir_1pin)
        app_config.raw_ok_dir_1pin = os.path.join(root_dir, r"raw_ok")
        dir_list.append(app_config.raw_ok_dir_1pin)
        app_config.raw_cut_dir_1pin = os.path.join(root_dir, r"raw_cut")
        dir_list.append(app_config.raw_cut_dir_1pin)
        app_config.totalboard_error_dir_1pin = os.path.join(root_dir, r"totalboard_error")
        dir_list.append(app_config.totalboard_error_dir_1pin)
        app_config.raw_fail_dir_1pin = os.path.join(root_dir, r"raw_fail")
        dir_list.append(app_config.raw_fail_dir_1pin)

    cutter_dir = r"C:\cutter_raw"
    cutter_dir = os.path.join(cutter_dir, r"{}\{}".format(get_datetime_for_logger_dir(), order_id))
    app_config.cutter_raw_begin_dir = os.path.join(cutter_dir, "begin_check")
    dir_list.append(app_config.cutter_raw_begin_dir)
    app_config.cutter_raw_begin_res_dir = os.path.join(cutter_dir, "begin_check_res")
    dir_list.append(app_config.cutter_raw_begin_res_dir)
    app_config.cutter_raw_final_dir = os.path.join(cutter_dir, "final_check")
    dir_list.append(app_config.cutter_raw_final_dir)
    app_config.cutter_raw_final_res_dir = os.path.join(cutter_dir, "final_check_res")
    dir_list.append(app_config.cutter_raw_final_res_dir)

    try:
        for file_dir in dir_list:
            if not os.path.exists(file_dir):
                os.makedirs(file_dir)
    except Exception as e:
        logger.debug(f"in  create_image_dir_by_order_id() error: {str(e)}")


if __name__ == '__main__':
    create_image_dir_by_order_id("test_order", "2")
